<?php
/**
 * 广告管理插件,可以管理banner广告,文字广告,支持分类,参考多个广告插件来开发.
 * date:2018-06-30
 * author: yoby
 */
require_once(dirname(__FILE__).'/config.php');
require_once(DEDEINC.'/datalistcp.class.php');
require_once(DEDEINC.'/common.func.php');
require_once(DEDEINC.'/Db.class.php');
setcookie('ENV_GOBACK_URL',$dedeNowurl,time()+3600,'/');

//初始化数据库操作pdo类,织梦自带数据库操作类太繁琐
$db = new Db($GLOBALS['cfg_dbhost'],$GLOBALS['cfg_dbuser'],$GLOBALS['cfg_dbpwd'],'3306',$GLOBALS['cfg_dbname'],$GLOBALS['cfg_dbprefix']);


$dopost = (empty($dopost))?"display":$dopost;

if("del" == $dopost){

    if(isset($arcid)) {
        $db->delete('yoby_adlist', "id IN ('".implode("','", $arcid)."')");
        ShowMsg("成功删除选中广告！","yoby_ad_main.php?tid=$tid");
        exit;
    }elseif($id>0){
        $db->delete('yoby_adlist',array('id'=>$id));
        ShowMsg("成功删除一个广告！","yoby_ad_main.php?tid=$tid");
        exit;
    }


}else if("post" == $dopost){
    $rs=array();
    if(!empty($id)){
        $rs =$db->get('yoby_adlist',array('id'=>$id));
    }

   if($_SERVER['REQUEST_METHOD'] == 'POST'){
       $litpic = $picname;
       $sendtime = time();
       $data= compact('title','litpic','tid','title1','description','url','sortrank','sendtime');

       if(empty($id)){

         $db->insert('yoby_adlist',$data);

       }   else{
           unset($data['sendtime']);
         $db->update('yoby_adlist',$data,array('id'=>$id));
       }
       ShowMsg("成功增加一个广告！","yoby_ad_main.php?tid=$tid");
       exit;

   }else{
       include DedeInclude('templets/yoby_ad_list.htm');
   }

}else if("isshow"==$dopost){
  $rs= $db->update('yoby_adlist',array('isshow'=>$is),array('id'=>$id));
    exit(json_encode(array('code'=>200)));
}

else if("display"==$dopost){
    $where = (empty($tid))?"and 1=1 ": "  and tid=$tid ";

    $sql = "select * from #@__yoby_adlist where 1=1 $where order by sortrank desc,id desc";

    $dl = new DataListCP();
    $dl->pageSize = 10;
    $dl->SetTemplate(DEDEADMIN."/templets/yoby_ad_list.htm");
    $dl->SetSource($sql);
    $dl->Display();
}

/**
 * 是否显示
 */
function isshow($id,$is){
    if(0==$is){
        $is=1;
        return "<a onclick='isshow($id,$is);' class=\"btn\">隐藏</a>";
    }else{
        $is=0;
        return "<a onclick='isshow($id,$is);' class=\"btn btn-green\">显示</a>" ;
    }
}
function title($tid){
    global $db;
   return $rs =$db->getcolumn('yoby_adtype',array('id'=>$tid),'typename');
}
function showimg($src){
    if(empty($src)){
        return "无图";
    }else{
      $arr =   getimagesize($src);
      if(empty($arr[0])){
          return '无图';
      }
        return  $arr[0]." X ".$arr[1];

    }
}